package com.example.player;

import android.opengl.GLSurfaceView;
import android.os.Environment;

import com.media.ffmpeg.FFMpegPlayer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class GL2JNIRenderer implements GLSurfaceView.Renderer, FFMpegPlayer.OnPlayerEventListener{
    FFMpegPlayer player ;
    private GL2JNIView mView;

    private int mRotate;

    public GL2JNIRenderer(GL2JNIView view) {
        this.mView = view;
        player = new FFMpegPlayer();
        player.setOnPlayerEventListener(this);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        player._setDataSource(Environment.getExternalStorageDirectory().getAbsolutePath() +
                "/DCIM/Camera/VID_20190404_192205.mp4");
        player._prepare();
        player._start();
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        player._initViewPort(mRotate, width, height);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        player._step();
    }

    @Override
    public void onVideoFrameUpdated() {
        mView.requestRender();
    }

    @Override
    public void onVideoSizeChanged(final int rotate, final int width, final int height) {
        mRotate = rotate;

        mView.post(new Runnable() {
            @Override
            public void run() {
                mView.setVideoWidth(width);
                mView.setVideoHeight(height);
                mView.requestLayout();
            }
        });

    }
}
